package com.wnx.job.modules.pms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.modules.jms.model.JmsCompany;
import com.wnx.job.modules.jms.service.JmsCompanyService;
import com.wnx.job.modules.pms.model.PmsAdvantage;
import com.wnx.job.modules.pms.model.PmsCompanyAdvantageRelation;
import com.wnx.job.modules.pms.service.PmsAdvantageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/pms/pmsAdvantage")
@Api(tags = "AdvantageController",description = "优势管理")
public class PmsAdvantageController {
    @Resource
    private PmsAdvantageService advantageService;
    @Resource
    private JmsCompanyService companyService;

    /**
     * 查询全部公司优势
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部优势")
    public ResultData<List<PmsAdvantage>> findAll(){
        log.info("======系统正在查询查询全部优势势中=======");
        List<PmsAdvantage> advantageList =  advantageService.list();
        log.info("======系统正在查询查询全部优势成功=======");
        return ResultData.success(Const.ADD_ADVANTAGE_SUCCESS,advantageList);
    }

    /**
     * 添加公司优势
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加公司优势")
    public ResultData<String> save(@RequestBody long[] advantageIds, Principal principal){
        log.info("============系统正在添加公司优势中==============");
        //获取前端的公司优势ID数组

        //通过认证主体，获取公司账号。
        String username = principal.getName();

        //调用服务层，添加公司优势
        advantageService.saveAdvantage(advantageIds,username);

        log.info("============系统成功添加公司优势！！！==============");
        return ResultData.success(Const.ADD_ADVANTAGE_SUCCESS);

    }

    /**
     * 获取该公司对应的公司福利
     * @return
     */
    @GetMapping("/findCompanyAdvantage")
    @ApiOperation("获取该公司对应的公司福利")
    public ResultData<Map<String, Object>> findCompanyAdvantage(Principal principal){
        //构造前端需要的该公司福利Ids集合
        List<Long> advantageIds = new ArrayList<Long>();

        //构造前端需要的该公司福利集合
        List<PmsAdvantage> advantageTags = new ArrayList<PmsAdvantage>();


        JmsCompany companyByDb = companyService.findByUsername(principal.getName());
        //调用服务层，根据公司ID查公司优势集合
        List<PmsCompanyAdvantageRelation> companyAdvantageList = advantageService
                .findCompanyAdvantageRelationsByCompanyId(companyByDb.getId());

        companyAdvantageList.forEach(companyAdvantage -> {
            Long advantageId = companyAdvantage.getAdvantageId();
            PmsAdvantage advantage =  advantageService.getById(advantageId);

            //完成数据采集
            advantageIds.add(advantageId);
            advantageTags.add(advantage);
        });

        //构造返回结果Map
        Map<String, Object> resultMap = new HashMap<String, Object>(2);
        resultMap.put("advantageIds",advantageIds);
        resultMap.put("advantageTags",advantageTags);


        //返回结果集对象
        return ResultData.success(Const.QUERY_ADVANTAGE_SUCCESS,resultMap);

    }







}

